package com.ruibang.glass.equipment.service.impl.device;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.domain.file.FolderInfo;
import com.ruibang.glass.equipment.domain.req.device.PressureVesselPageReq;
import com.ruibang.glass.equipment.domain.req.device.TechnicalDocumentReq;
import com.ruibang.glass.equipment.dto.SelectDTO;
import com.ruibang.glass.equipment.entity.device.EquipmentDeviceInfo;
import com.ruibang.glass.equipment.entity.device.TechnicalDocumentManagement;
import com.ruibang.glass.equipment.feign.FileApi;
import com.ruibang.glass.equipment.mapper.device.TechnicalDocumentManagementMapper;
import com.ruibang.glass.equipment.service.EquipmentDeviceInfoService;
import com.ruibang.glass.equipment.service.TechnicalDocumentManagementService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文档管理服务实现类
 * </p>
 *
 * @author mameng
 * @since 2024-03-20
 */
@Service
public class TechnicalDocumentManagementServiceImpl extends ServiceImpl<TechnicalDocumentManagementMapper, TechnicalDocumentManagement> implements TechnicalDocumentManagementService {

    @Autowired
    private FileApi fileApi;

    /**
     * 文档管理服列表
     *
     * @param pressureVesselPageReq
     * @return
     */
    @Override
    public Page<TechnicalDocumentManagement> queryPage(TechnicalDocumentReq technicalDocumentReq) {
        return baseMapper.getPageList(new Page<>(technicalDocumentReq.getPageNo(), technicalDocumentReq.getPageSize()), technicalDocumentReq);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultBody add(TechnicalDocumentManagement technicalDocumentManagement) {
        //保存文档
        this.save(technicalDocumentManagement);

        //存储文件夹信息
        List<FolderInfo> folderInfos = technicalDocumentManagement.getFolderInfos();
        if (CollectionUtils.isNotEmpty(folderInfos)) {
            for (FolderInfo folderInfo : folderInfos) {
                folderInfo.setBusinessId(technicalDocumentManagement.getId());
            }
        }
        return fileApi.upload(folderInfos);
    }


    @Override
    public ResultBody getDeviceList() {
        List<EquipmentDeviceInfo> deviceInfoList = baseMapper.configurableList();
        List<SelectDTO> selectDTOList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(deviceInfoList)) {
            for (EquipmentDeviceInfo device : deviceInfoList) {
                SelectDTO selectDTO = new SelectDTO();
                selectDTO.setValue(device.getId());
                selectDTO.setLabel(device.getDeviceName());
                selectDTO.setDisabled(device.getDisabled());
                selectDTOList.add(selectDTO);
            }
        }
        return ResultBody.success(selectDTOList);
    }

    @Override
    public ResultBody deleteById(String id) {
        return null;
    }


}
